home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_128 / mrbackup / unixwild.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  122 lines

  1. /* unixwild.c
  2.  * Unix (tm) wildcard support routines. 
  3.  * Created on 08/07/87
  4.  */
  5.  
  6. /* Text a filename for wildcard content.
  7.  * Called with:
  8.  *        name:        pathname
  9.  * Returns:
  10.  *        0 => no wild characters
  11.  *        1 => 1 or more wild characters
  12.  */
  13. int
  14. iswild(name)
  15.     char *name;
  16. {
  17.     register char c, *s;
  18.  
  19.     for (s = name; c = *s++; )
  20.         if (c == '*' || c == '?') return 1;
  21.  
  22.     return 0;
  23. }
  24.  
  25.  
  26. /*
  27.  * Compare a wild card name with a normal name.
  28.  * Called with:
  29.  *        wild:        name with "wild" content
  30.  *        name:        "normal" name
  31.  * Returns:
  32.  *        0 => names don't match
  33.  *        1 => names match
  34.  *
  35.  * This source was lifted from Steve Drew's version of Matt Dillon's
  36.  * shell, version 2.06m.
  37.  * 
  38.  */
  39.  
  40. #define MAXB   8
  41.  
  42. int
  43. wildcmp(wild,name)
  44.     char *wild,*name;
  45. {
  46.     register char  *w = wild;
  47.     register char  *n = name;
  48.     char   *back[MAXB][2];
  49.     register char   s1,s2;
  50.     int     bi = 0;
  51.  
  52.     while (*n || *w){
  53.         switch (*w){
  54.             case '*': 
  55.                 if (bi == MAXB){
  56. #ifdef DEBUG
  57.                     printf("Too many levels of '*'\n");
  58. #endif
  59.                     return (0);
  60.                 }
  61.                 back[bi][0]= w;
  62.                 back[bi][1]= n;
  63.                 ++bi;
  64.                 ++w;
  65.                 continue;
  66.         goback: 
  67.                 --bi;
  68.                 while (bi >= 0 && *back[bi][1]== '\0')
  69.                     --bi;
  70.                 if (bi < 0)
  71.                     return (0);
  72.                 w = back[bi][0]+ 1;
  73.                 n = ++back[bi][1];
  74.                 ++bi;
  75.                 continue;
  76.             case '?': 
  77.                 if (!*n){
  78.                     if (bi)
  79.                         goto goback;
  80.                     return (0);
  81.                 }
  82.                 break;
  83.             default: 
  84.                 s1 = (*n >= 'A' && *n <= 'Z')?*n - 'A' + 'a' :*n;
  85.                 s2 = (*w >= 'A' && *w <= 'Z')?*w - 'A' + 'a' :*w;
  86.                 if (s1 != s2){
  87.                     if (bi)
  88.                         goto goback;
  89.                     return (0);
  90.                 }
  91.                 break;
  92.         }
  93.         if (*n)
  94.             ++n;
  95.         if (*w)
  96.             ++w;
  97.     }
  98.     return (1);
  99. }
  100. #ifdef DEBUG
  101. char normal[81], wild[81];
  102. main()
  103. {
  104.     puts("Terminate this program by entering 'quit'");
  105.     for (;;) {
  106.         puts("Enter the non-wild pathname:");
  107.         gets(normal);
  108.         if (!strcmp(normal,"quit")) break;
  109.         if (iswild(normal)) {
  110.             puts("No, idiot!  Enter a non-wild filename!");
  111.             continue;
  112.         }
  113.         puts("Enter a wild pathname:");
  114.         gets(wild);
  115.         if (wildcmp(wild,normal))
  116.             puts("Yup, they match.");
  117.         else
  118.             puts("No match here.");
  119.     }
  120. }
  121. #endif
  122.